﻿using BLL;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Thksoft.MXReport
{
    public partial class PlanWeekDetailReport : BaseUI.BasePage
    {
        private QS_PLANWEEKAPPLYBLL wBll = new QS_PLANWEEKAPPLYBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtDate.Value = DateTime.Now.ToString("yyyy-MM");
            }
        }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
        }

        protected void btnOutput_Click(object sender, EventArgs e)
        {
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            var orderStr = ViewState["ORDER"] as string;
            var orderTypeStr = ViewState["ORDER_TYPE"] as string;

            if (txtDate.Value.Trim() == "")
            {
                MessageBox.Show(Page, "请选择计划年月！");
                return;
            }

            int year = int.Parse(txtDate.Value.Substring(0, 4));

            int month = int.Parse(txtDate.Value.Substring(4));

            decimal comid = lognUser.USER_COMPANY;
            if (ddlCom.Value != -1)
            {
                comid = ddlCom.Value;
            }

            DataTable dt = wBll.GetDataByBid(year, month, comid, lognUser.USER_NAME, orderStr, orderTypeStr);

            grvShow1.DataSource = dt;
            grvShow1.DataBind();
        }

        protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string buid = ((Label)e.Row.FindControl("lblBuid")).Text;
                if (buid == "0")
                {
                    ((Panel)e.Row.FindControl("Panel2")).Visible = false;
                }
            }
        }

        /// <summary>
        /// 列排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["ORDER"] == null ? null : (string)ViewState["ORDER"];
            OrderDire = ViewState["ORDER_TYPE"] == null ? null : (string)ViewState["ORDER_TYPE"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "DESC")
                    OrderDire = "ASC";
                else
                    OrderDire = "DESC";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["ORDER"] = SortOrder;
            ViewState["ORDER_TYPE"] = OrderDire;
            btnSelect_Click(sender, e);
        }

        //protected void btnPrint_Click1(object sender, EventArgs e)
        //{
        //    toExecl1(Panel1, txtDate.Value + "周计划明细");
        //}

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrint_Click1(object sender, EventArgs e)
        {
            var orderStr = ViewState["ORDER"] as string;
            var orderTypeStr = ViewState["ORDER_TYPE"] as string;

            if (txtDate.Value == "" || grvShow1 == null || grvShow1.Rows.Count == 0)
            {
                MessageBox.Show(Page, "【系统提示】未能查询出数据，暂时不能导出，请先查询数据。");
                return;
            }

            int year = int.Parse(txtDate.Value.Substring(0, 4));

            int month = int.Parse(txtDate.Value.Substring(4));

            decimal comid = lognUser.USER_COMPANY;
            if (ddlCom.Value != -1)
            {
                comid = ddlCom.Value;
            }

            DataTable dt = wBll.GetDataByBid(year, month, comid, lognUser.USER_NAME, orderStr, orderTypeStr);

            var html = "<table class='cont_list' cellspacing='0' rules='all' border='1' style='width: 100%;border-collapse: collapse;'>";
            html += "<tr>";
            html += "<th>公司名称</th>";
            html += "<th>业务名称</th>";
            html += "<th>周数</th>";
            html += "<th>合计（流入）</th>";
            html += "<th>合计（流出）</th>";
            html += "</tr>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                html += "<tr>";

                if (i % 2 == 0)
                {
                    html += "<td style=\"background-color:#EAFFFF;\">" + dt.Rows[i]["cmy_name"].ToString() + "</td>";
                    html += "<td style=\"background-color:#EAFFFF;\">" + dt.Rows[i]["c_week_name"].ToString() + "</td>";
                    html += "<td style=\"background-color:#EAFFFF;\">" + dt.Rows[i]["n_week"].ToString() + "</td>";
                    html += "<td style=\"background-color:#EAFFFF;\">" + dt.Rows[i]["流入合计"].ToString() + "</td>";
                    html += "<td style=\"background-color:#EAFFFF;\">" + dt.Rows[i]["流出合计"].ToString() + "</td>";
                }
                else
                {
                    html += "<td>" + dt.Rows[i]["cmy_name"].ToString() + "</td>";
                    html += "<td>" + dt.Rows[i]["c_week_name"].ToString() + "</td>";
                    html += "<td>" + dt.Rows[i]["n_week"].ToString() + "</td>";
                    html += "<td>" + dt.Rows[i]["流入合计"].ToString() + "</td>";
                    html += "<td>" + dt.Rows[i]["流出合计"].ToString() + "</td>";
                }

                html += "</tr>";
            }

            //html += "<tr>";
            //html += "<td colspan=\"3\" align=\"center\">合计</td>";
            //html += "<td>" + dt.AsEnumerable().Sum(a => Convert.ToDecimal(a["流入合计"])) + "</td>";
            //html += "<td>" + dt.AsEnumerable().Sum(a => Convert.ToDecimal(a["流出合计"])) + "</td>";
            //html += "<td></td>";
            //html += "</tr>";
            html += "</table>";
            toExecl1(html, (txtDate.Value == "" ? "" : txtDate.Value + "周") + "资金明细");
        }
    }
}